﻿latbegin = 10.771828429090126;
logbegin = 106.70473337173462;
Map2Addr = false;
Addr2Map = false;
Search = false;
flagTemp = false;
function end(lat, log, addr, setCenter) {
   
    if (setCenter == true)
        marker.setLatLng(center);
    $(lat).val(marker.getLatLng().lat());
    $(log).val(marker.getLatLng().lng());

    geocoder.getLocations(marker.getLatLng(), function (addresses) {
        if (addresses.Status.code != 200) {
            //alert("reverse geocoder failed to find an address for " + center.toUrlValue());
        }
        else {
            address = addresses.Placemark[0];
            $myHtml = address.address;
            if (Map2Addr == true) {
                $(addr).val($myHtml);
            }

            marker.openInfoWindowHtml($myHtml);
            map.setCenter(marker.getLatLng());
        }
    });
}
function initializeGmarker(idmap, _lat, _log, _addr) {

    lat = _lat;
    log = _log;
    addr = _addr;
    if (window.GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById(idmap));
        center = new GLatLng(latbegin, logbegin);
        map.setCenter(center, 16);

        geocoder = new GClientGeocoder();
        map.setUIToDefault();


        map.enableGoogleBar();
        // Enable the additional map types within
        // the map type collection
        map.enableRotation();
        marker = new GMarker(center, { draggable: true });

        GEvent.addListener(marker, "dragstart", function () {
            
            map.closeInfoWindow();
        });
        GEvent.addListener(marker, "click", function () {
            end(lat, log,addr, false);
        });
        GEvent.addListener(marker, "dragend", function () {
           
            end(lat, log, addr,false);
        });


        map.addOverlay(marker);
        end(lat, log, addr, false);
                 
        }



}
function initializeGmarkerForEdit(idmap, _lat, _log, _addr, latlg, loglg,addlg) {

    lat = _lat;
    log = _log;
    addr = _addr;
    if (window.GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById(idmap));
        center = new GLatLng(latlg, loglg);
        map.setCenter(center, 16);

        geocoder = new GClientGeocoder();
        map.setUIToDefault();


        map.enableGoogleBar();
        // Enable the additional map types within
        // the map type collection
        map.enableRotation();
        marker = new GMarker(center, { draggable: true });

        GEvent.addListener(marker, "dragstart", function () {

            map.closeInfoWindow();
        });
        GEvent.addListener(marker, "click", function () {
            end(lat, log, addr, false);
        });
        GEvent.addListener(marker, "dragend", function () {

            end(lat, log, addr, false);
        });


        map.addOverlay(marker);
        marker.openInfoWindowHtml(addlg);

    }



}
function SetLocation(NumLat, NumLog) {

    center = new GLatLng(NumLat, NumLog);
    end(lat, log, addr, true);
}

function GetYourLocation() {
    if (navigator.geolocation) {

        navigator.geolocation.getCurrentPosition(function (position) {

            center = new GLatLng(position.coords.latitude, position.coords.longitude);
            marker.closeInfoWindow();

            end(lat, log, addr, true);
        }, function (error) {
        });
    }
}
function initialize(idmap,address, NumLat, NumLog) {
    if (GBrowserIsCompatible()) {


        map = new GMap2(document.getElementById(idmap));
        var center = new GLatLng(NumLat, NumLog);

        geocoder = new GClientGeocoder();
        map.setUIToDefault();

        map.setCenter(center, 15);
        map.enableGoogleBar();
        // Enable the additional map types within
        // the map type collection
        map.enableRotation();
        marker = new GMarker(center, { draggable: false });
        GEvent.addListener(marker, "click", function () {
            marker.openInfoWindowHtml(address);
        });


        map.addOverlay(marker);
        map.openInfoWindowHtml(map.getCenter(), address);
    }
}
$(document).ready(function () {

    $("#Map2Addr").change(function () {
        if ($('#Map2Addr').is(':checked')) {
            Map2Addr = true;

        }
        else {
            Map2Addr = false;
        }

    });
    $("#Addr2Map").change(function () {
        if ($('#Addr2Map').is(':checked')) {
            Addr2Map = true;
        }
        else {
            Addr2Map = false;
        }

    });
    $("#AddressReceive").keyup(function () {
        $("#Map2Addr").attr('checked', false);
        if (Map2Addr == true) {
            flagTemp = true;
            Map2Addr = false;
        }
        Address2Coordinates();

    });
    $("#AddressReceive").blur(function () {

        Address2Coordinates();
        if (flagTemp == true) {
            flagTemp = false;
            $("#Map2Addr").attr('checked', true);
            Map2Addr = true;
        }

    });
    $("#AddressReceive").focus(function () {


        $("#Map2Addr").attr('checked', false);
        if (Map2Addr == true) {
            flagTemp = true;
            Map2Addr = false;
        }
    });
    $("#map_canvas").click(function () {
        if (flagTemp == true) {
            flagTemp = false;
            $("#Map2Addr").attr('checked', true);
            Map2Addr = true;
        }
    });

    $("#SelectCenter").click(function () {
        center = map.getCenter();
        end(lat, log, true);
    });
    $("#YourLocation").click(function () {
        GetYourLocation();
    });
});

function Address2Coordinates() {
    if (Search == false) {

        if (Addr2Map == true) {
            Search = true;
            var $html1 = $("#AddressReceive").val();
            geocoder.getLatLng($html1, function (position) {
                Search = false;
                if (!position) {

                } else {

                    center = position;
                    end(lat, log,addr, true);

                }
                
            });
        }
    } 
}